package cn.itcast.service;


import cn.itcast.mapper.OrderSettingMapper;
import cn.itcast.pojo.OrderSetting;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Override
    public void add(List<OrderSetting> orderSettingList) {

        if (orderSettingList != null && orderSettingList.size() > 0) {
            for (OrderSetting orderSetting : orderSettingList) {
                long count = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());
                if (count == 0) {
                    orderSettingMapper.add(orderSetting);
                } else {
                    orderSettingMapper.editNumberByOrderDate(orderSetting);
                }

            }
        }

    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {//2019-10
        String start = date + "-1";
        String end = date + "-31";
        Map<String,String> param = new HashMap<>();
        param.put("start",start);
        param.put("end",end);
        List<Map> orderSettings = orderSettingMapper.getOrderSettingByMonth(param);
        return orderSettings;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        long count = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());
        if (count == 0) {
            orderSettingMapper.add(orderSetting);
        } else {
            orderSettingMapper.editNumberByOrderDate(orderSetting);
        }
    }

    @Override
    public OrderSetting findByOrderDate(Date date) {
        return orderSettingMapper.findByOrderDate(date);
    }

}
